package com.aoip.telemetry.telemetry.configureui.framework.web.controller;

import com.aoip.telemetry.telemetry.configureui.common.constant.Constants;
import com.aoip.telemetry.telemetry.configureui.common.core.domain.entity.SysMenu;
import com.aoip.telemetry.telemetry.configureui.common.core.domain.entity.SysUser;
import com.aoip.telemetry.telemetry.configureui.common.core.domain.model.LoginBody;
import com.aoip.telemetry.telemetry.configureui.common.utils.SecurityUtils;
import com.aoip.telemetry.telemetry.configureui.framework.config.properties.CasProperties;
import com.aoip.telemetry.telemetry.configureui.framework.web.service.SysLoginService;
import com.aoip.telemetry.telemetry.configureui.framework.web.service.SysPermissionService;
import com.aoip.telemetry.telemetry.configureui.system.service.ISysMenuService;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 登录验证
 * 
 * @author ss
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private CasProperties casProperties;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 登录中转
     * @param
     * @return 结果
     */
    @GetMapping("/loginCas")
    public void loginCas(HttpServletResponse response, HttpServletRequest httpServletRequest) throws IOException {
        response.sendRedirect(casProperties.getWebUrl());
    }

    /**
     * 登录方法
     *
     * @param request 登录信息
     * @return 结果
     */
    @PostMapping("/doLogin")
    public AjaxResult doLogin(HttpServletRequest request)
    {
        AjaxResult ajax = AjaxResult.success();
        if (request.getUserPrincipal() != null) {
            AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
            Map attributes = principal.getAttributes();
            String userCode = (String) attributes.get("userCode");
            String userName = (String) attributes.get("userName");
            String mobile = (String) attributes.get("mobile");
            System.out.println(userCode+"--------------"+userName+"---"+mobile);
            // 生成令牌
            String token = loginService.loginCas(attributes);
            ajax.put(Constants.TOKEN, token);
        }
        return ajax;
    }


    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
